
#include "App.h" 
#include "OS.h" 
#include "utils.h" 
#include "menu_msg.h" 
#include "macros.h" 
#include "debug.h" 
#include "SysClock.h" 
#include "string.h" 
#include "memmap.h" 
#include "apps.h" 
#include "storage.h" 


int cnt;
char passw[16];
void app_change_password ( void )
{
	if ( APP.Signal == APP_SIGNAL_CLOSE )
		APP.State = ASTE_FINISH;

	switch( APP.State )
	{
		case ASTE_START:
			DBG("\n%s; started\n",__FUNCTION__); 
			cnt = 0;
			APP.Result = AR_FAIL;
			APP.State = ASTE_IDLE;
			OS_SET_TIMER(15000,ASTE_FINISH);
			enter_passw_inf.addr_passw = ADDR_PASSW_1;
			OS_Call_App(1,util_enter_password);
			break;
			
		case ASTE_IDLE:
			if ( APP.CallResult == AR_SUCCSESS )
			{
				DBG("\n%s; ===> cnt: %d\n",__FUNCTION__,cnt); 
				APP.CallResult = 0;

				OS_SET_TIMER(15000,ASTE_FINISH);

				if ( cnt == 2 )   /* twice enter of new password */
				{
					if ( StrCmp(passw, util_enter_data_inf.data) == 0 )
					{
						APP.Result = AR_SUCCSESS;
						msg_play__msg = AMSG_ENTER_NEW_PASSWORD_OK;
						storage.Write(ADDR_PASSW_1,passw,sizeof(passw)+1);
					}
					else
					{
						msg_play__msg = AMSG_PASSWORD_WRONG;
						APP.Result = AR_FAIL;
					}

					APP.State = ASTE_FINISH;

					OS_Call_App(0,msg_play); 
				}
				else 
				{
					if ( cnt == 1 )
					{
						StrCpy(passw, util_enter_data_inf.data);
					}

					util_enter_data_inf.msg_to_play = AMSG_ENTER_NEW_PASSWORD;
					DBG("\n%s; util_enter_data_inf.msg_to_play = 0x%X\n",__FUNCTION__,util_enter_data_inf.msg_to_play); 
					OS_Call_App(1,util_enter_data);
					cnt++;
				}
			}
			else
			{
				APP.Result = AR_FAIL;
				APP.State = ASTE_FINISH;
			}
			break;	

		case ASTE_FINISH:
			DBG("\n%s; FINISH\n",__FUNCTION__); 
			//OS_Send_Signal(util_enter_password, APP_SIGNAL_CLOSE);
			OS_Return();
	}
}

/* void app_output_name_record ( void ) */
/* { */
/* 	if ( APP.Signal == APP_SIGNAL_CLOSE ) */
/* 		APP.State = ASTE_FINISH; */

/* 	switch( APP.State ) */
/* 	{ */
/* 		case ASTE_START: */
/* 			DBG("\n%s; started\n",__FUNCTION__);  */
/* 			cnt = 0; */
/* 			APP.Result = AR_FAIL; */
/* 			APP.State = ASTE_IDLE; */
/* 			OS_SET_TIMER(15000,ASTE_FINISH); */
/* 			enter_passw_inf.addr_passw = ADDR_PASSW_1; */
/* 			OS_Call_App(1,util_enter_password); */
/* 			break; */
			
/* 		case ASTE_IDLE: */
/* 			break; */
/* 	} */
/* } */
